/**
 * 
 */
package edu.cmu.cs.webapp.ioms.ui.dialogs.log;

import java.text.DateFormat;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import edu.cmu.cs.webapp.ioms.dao.DataAccessException;
import edu.cmu.cs.webapp.ioms.dao.impl.LogDAOImpl;
import edu.cmu.cs.webapp.ioms.models.LineItem;
import edu.cmu.cs.webapp.ioms.models.LogEntry;
import edu.cmu.cs.webapp.ioms.utils.IOMSImageHelper;
import edu.cmu.cs.webapp.ioms.utils.IOMSImages;

/**
 * @author Christian
 * 
 */
public class ViewLogsDialog extends Dialog {

	private LogEntry[] logs;
	private TableViewer logsViewer;
	private Table table;

	public ViewLogsDialog(Shell parentShell) {
		super(parentShell);
		try {
			this.logs = new LogDAOImpl().retrieveLogs()
					.toArray(new LogEntry[0]);
		} catch (DataAccessException e) {
		}
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite composite = (Composite) super.createDialogArea(parent);

		GridLayout gl = new GridLayout(1, false);
		composite.setLayout(gl);

		createTableViewer(composite);
		return composite;
	}

	@Override
	protected void configureShell(final Shell shell) {
		super.configureShell(shell);
		shell.setText("Logs");
		shell.setSize(800, 400);
	}

	private void createTableViewer(Composite container) {

		/*
		 * Create table
		 */
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;

		table = new Table(container, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION);

		table.setLayoutData(gridData);

		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		// Log type
		TableColumn productColumn = new TableColumn(table, SWT.NONE);
		productColumn.setText("Log Type"); //$NON-NLS-1$
		productColumn.setWidth(110);

		TableColumn pacColumn = new TableColumn(table, SWT.NONE);
		pacColumn.setText("Processed By"); //$NON-NLS-1$
		pacColumn.setWidth(90);

		// Price per unit
		TableColumn pricColumn = new TableColumn(table, SWT.NONE);
		pricColumn.setText("Log Time"); //$NON-NLS-1$
		pricColumn.setWidth(90);

		TableColumn quantityColumn = new TableColumn(table, SWT.NONE);
		quantityColumn.setText("Log Message"); //$NON-NLS-1$
		quantityColumn.setWidth(1000);

		/*
		 * Create viewer
		 */
		logsViewer = new TableViewer(table);

		logsViewer.setUseHashlookup(true);
		logsViewer.setLabelProvider(new LogsViewerLabelProvider());
		logsViewer.setContentProvider(new LogsTableViewerContentProvider());

		logsViewer.setInput(logs);

	}

	private class LogsViewerLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		// table column index
		public static final int TYPE = 0;
		public static final int PROCESSED_BY = 1;
		public static final int TIME = 2;
		public static final int MESSAGE = 3;

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java
		 * .lang .Object, int)
		 */
		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			if (element instanceof LogEntry) {
				LogEntry entry = (LogEntry) element;
				Image img = null;
				if (entry.getLog_type().equals("Add")) {
					img = IOMSImageHelper.getInstance().getImage(
							IOMSImages.IMG_LOG_ADD);
				} else if (entry.getLog_type().equals("Update")) {
					img = IOMSImageHelper.getInstance().getImage(
							IOMSImages.IMG_LOG_UPDATE);
				} else if (entry.getLog_type().equals("Delete")) {
					img = IOMSImageHelper.getInstance().getImage(
							IOMSImages.IMG_LOG_DELETE);
				}

				if (columnIndex == TYPE) {
					return img;
				}
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
		 * .Object, int)
		 */
		@Override
		public String getColumnText(Object element, int columnIndex) {
			if (element instanceof LogEntry) {
				LogEntry entry = (LogEntry) element;
				switch (columnIndex) {
				// case ITEM_ID:
				// // id
				// return "#" + item.getItem_id();
				case TYPE:
					// type
					return entry.getLog_type() == null ? "" : entry
							.getLog_type();
				case MESSAGE:
					// quantity
					return entry.getLog_msg() == null ? "" : entry.getLog_msg();
				case PROCESSED_BY:
					// unit
					return entry.getUser();
				case TIME:
					// unit price
					return entry.getTime() == null ? "" : DateFormat
							.getDateTimeInstance().format(entry.getTime());
				default:
					return "";
				}
			}

			return "";

		}
	}

	private class LogsTableViewerContentProvider implements
			IStructuredContentProvider {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java
		 * .lang.Object)
		 */
		public Object[] getElements(Object parentElement) {

			if (parentElement instanceof LogEntry[]) {
				return (LogEntry[]) parentElement;
			}

			return new Object[0];
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
		 */
		public void dispose() {

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse
		 * .jface .viewers.Viewer, java.lang.Object, java.lang.Object)
		 */
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}

	}

}
